/**
 * 
 */
package nl.sportan.samenloop;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import nl.sportan.samenloop.adapter.TileAdapter;
import nl.sportan.samenloop.communication.PhotoListTarget;
import nl.sportan.samenloop.communication.PhotoTarget;
import nl.sportan.samenloop.communication.flickr.FrontPageRetriever;
import nl.sportan.samenloop.communication.flickr.PhotoDataRetriever;
import nl.sportan.samenloop.model.Tile;
import nl.sportan.samenloop.timers.CountDownPanel;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.gmail.yuyang226.flickr.photos.Photo;
import com.gmail.yuyang226.flickr.photos.PhotoList;
import com.gmail.yuyang226.flickr.photos.Size;

/**
 * @author Bram Wiekens
 * 
 */
public class SamenLoopMainActivity extends Activity implements PhotoTarget {

	private Handler handler;
	// TODO read from filesystem
	private List<Bitmap> photos;

	private ImageView[] imageViews;
	private int lastImage = 0;

	public SamenLoopMainActivity() {
		super();
		photos = new ArrayList<Bitmap>();
		handler = new Handler();
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

		setContentView(R.layout.main);

		LinearLayout layout = (LinearLayout) findViewById(R.id.listView);

		fillGrid();

		imageViews = new ImageView[2];
		imageViews[0] = (ImageView) findViewById(R.id.imagereal1);
		imageViews[1] = (ImageView) findViewById(R.id.imagereal2);

		layout.addView(createCoundDown());

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);

		Configuration c= getResources().getConfiguration();
		
		if (c.orientation == Configuration.ORIENTATION_PORTRAIT) {
			View v = findViewById(R.id.image_container);
			v.setVisibility(View.VISIBLE);
			getPhotos();
		} else {
			View v = findViewById(R.id.image_container);
			v.setVisibility(View.GONE);
		}
		
		
	}


	private void getPhotos() {
		FrontPageRetriever fpr = new FrontPageRetriever(new PhotoRetriever());

		Thread t = new Thread(fpr);
		t.start();
	}

	private class PhotoRetriever implements PhotoListTarget {

		public void setPhotoList(PhotoList photoset) {
			for (Photo photo : photoset) {
				handler.post(new PhotoDataRetriever(SamenLoopMainActivity.this, photo, Size.MEDIUM,
						SamenLoopMainActivity.this, true));
			}

		}

	}

	private class PhotoFlipper implements Runnable {

		public void run() {
				lastImage = (lastImage + 1) % photos.size();
				
				int viewNum = lastImage % 2;
				int prevView = Math.abs((viewNum - 1) %2);
			
				imageViews[viewNum].setImageBitmap(photos.get(lastImage));
			
				Animation fadeOutAnimation = AnimationUtils.loadAnimation(SamenLoopMainActivity.this, android.R.anim.fade_out);
				imageViews[prevView].startAnimation(fadeOutAnimation);
				imageViews[prevView].setVisibility(View.GONE);
				
				Animation fadeInAnimation = AnimationUtils.loadAnimation(SamenLoopMainActivity.this, android.R.anim.fade_in);
				imageViews[viewNum].startAnimation(fadeInAnimation);
				imageViews[viewNum].setVisibility(View.VISIBLE);
				
				
				handler.postDelayed(new PhotoFlipper(), 10000);
		}
	}

	private void initFlipper() {
		if (imageViews[0].getVisibility() == View.GONE) {
			View vi = findViewById(R.id.progress);
			Animation fadeOutAnimation = AnimationUtils.loadAnimation(SamenLoopMainActivity.this, android.R.anim.fade_out);
			vi.startAnimation(fadeOutAnimation);
			vi.setVisibility(View.GONE);

			imageViews[0].setImageBitmap(photos.get(0));
			
			Animation fadeInAnimation = AnimationUtils.loadAnimation(SamenLoopMainActivity.this, android.R.anim.fade_in);
			imageViews[0].startAnimation(fadeInAnimation);
			imageViews[0].setVisibility(View.VISIBLE);
		}

		handler.postDelayed(new PhotoFlipper(), 10000);
	}

	private void fillGrid() {
		GridView tiles = (GridView) findViewById(R.id.tiles);
		
		tiles.setAdapter(new TileAdapter(this));

		tiles.setOnItemClickListener(new TileListener());

	}

	private View createCoundDown() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 30);
		cal.set(Calendar.MONTH, 5);
		cal.set(Calendar.YEAR, 2012);
		cal.set(Calendar.HOUR, 15);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);

		CountDownPanel cdp = new CountDownPanel(getApplicationContext(),
				cal.getTime());
		cdp.setGravity(Gravity.CENTER);

		return cdp;
	}

	private class TileListener implements OnItemClickListener {

		public void onItemClick(AdapterView<?> adapter, View view,
				int position, long id) {
			Tile t = (Tile) adapter.getItemAtPosition(position);

			Intent i = new Intent(SamenLoopMainActivity.this, t.getActivity());
			startActivity(i);

		}

	}

	public void setPhoto(Bitmap bitmap) {
		synchronized (photos) {
			photos.add(bitmap);

			if (photos.size() == 1) {
				initFlipper();
			}
		}
	}

}
